////////////////////////////////////////////////////////////////////////
//
// dialog_register.html
//
////////////////////////////////////////////////////////////////////////

var myListURL = "http://minsus.com/app/secret-box/msg_mylist.php?user=01042322595";

$('#mylist').live('pageinit',function(event)
{

//                  alert(myListURL);
//    document.getElementById("myNumber").innerHTML = addHyphen(myPhoneNum);
                  
                  setTimeout(function(){
//                             alert("asdlkjasdflkjasdfl;kj" + window.innerHeight + "_" +  $('#mailToBtn').offset().top + "_" + $('#mailToBtn').height() + "_" + $('#mailToBtn').offset().bottom);
                             $('#wrapper').height(window.innerHeight - ($('#mailToBtn').offset().top + $('#mailToBtn').height()));
                             
                             refreshList();
                             
                             //                  document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
                            // loaded();
                             
                             }, 800);
//                  document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);
    
});


var myScroll,
pullDownEl, pullDownOffset,
pullUpEl, pullUpOffset,
generatedCount = 0;

function pullDownAction () {
	setTimeout(function () {	// <-- Simulate network congestion, remove setTimeout from production!
               var el, li, i;
               el = document.getElementById('thelist');
               
               for (i=0; i<3; i++) {
               li = document.createElement('li');
               li.innerText = 'Generated row ' + (++generatedCount);
               el.insertBefore(li, el.childNodes[0]);
               }
               
               myScroll.refresh();		// Remember to refresh when contents are loaded (ie: on ajax completion)
               }, 1000);	// <-- Simulate network congestion, remove setTimeout from production!
}

//function pullUpAction () {
//	setTimeout(function () {	// <-- Simulate network congestion, remove setTimeout from production!
//               var el, li, i;
//               el = document.getElementById('thelist');
//               
//               for (i=0; i<3; i++) {
//               li = document.createElement('li');
//               li.innerText = 'Generated row ' + (++generatedCount);
//               el.appendChild(li, el.childNodes[0]);
//               }
//               
//               myScroll.refresh();		// Remember to refresh when contents are loaded (ie: on ajax completion)
//               }, 1000);	// <-- Simulate network congestion, remove setTimeout from production!
//}

function loaded() {
//	pullDownEl = document.getElementById('pullDown');
//	pullDownOffset = pullDownEl.offsetHeight;
////	pullUpEl = document.getElementById('pullUp');	
////	pullUpOffset = pullUpEl.offsetHeight;
//	
//	myScroll = new iScroll('wrapper'
//                           , {
//                           useTransition: true,
//                           topOffset: pullDownOffset,
//                           onRefresh: function () {
//                           if (pullDownEl.className.match('loading')) {
//                           pullDownEl.className = '';
//                           pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';
//                           } 
////                           else if (pullUpEl.className.match('loading')) {
////                           pullUpEl.className = '';
////                           pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
////                           }
//                           },
//                           onScrollMove: function () {
//                           if (this.y > 5 && !pullDownEl.className.match('flip')) {
//                           pullDownEl.className = 'flip';
//                           pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Release to refresh...';
//                           this.minScrollY = 0;
//                           } else if (this.y < 5 && pullDownEl.className.match('flip')) {
//                           pullDownEl.className = '';
//                           pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Pull down to refresh...';
//                           this.minScrollY = -pullDownOffset;
//                           } 
////                           else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
////                           pullUpEl.className = 'flip';
////                           pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Release to refresh...';
////                           this.maxScrollY = this.maxScrollY;
////                           } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
////                           pullUpEl.className = '';
////                           pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Pull up to load more...';
////                           this.maxScrollY = pullUpOffset;
////                           }
//                           },
//                           onScrollEnd: function () {
//                           if (pullDownEl.className.match('flip')) {
//                           pullDownEl.className = 'loading';
//                           pullDownEl.querySelector('.pullDownLabel').innerHTML = 'Loading...';				
//                           pullDownAction();	// Execute custom function (ajax call?)
//                           }
////                           else if (pullUpEl.className.match('flip')) {
////                           pullUpEl.className = 'loading';
////                           pullUpEl.querySelector('.pullUpLabel').innerHTML = 'Loading...';				
////                           pullUpAction();	// Execute custom function (ajax call?)
////                           }
//                           }
//                           }
//                           );
//    $(document).ready(function(){
    myScroll = new iScroll('wrapper', {vScrollbar:false, hScrollbar:false, snap:'li', minScrollY:15, momentum:true, 
                           onRefresh: function () {
                           this.minScrollY = 15;
                           this.maxScrollY = -($('#myListView').height() - 15 - $('#wrapper').height());
//                           alert($('#wrapper').height() + "_" + $('#myListView').height() + "_" + this.minScrollY + "_" + this.maxScrollY);
                           
                           },
                           
                           
                           onScrollEnd:function(){
                           
//                           alert('scrollEnd!!' + "_" + this.maxScrollY + "_" + this.minScrollY);
                           
                           
                           
                           }});
                      
//                      });
	
	setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
}

//document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);



function refreshList()
{
    $.ajax({
        url: myListURL,
        type: "GET",
        success: function(jsonData){
            //           alert(jsonData);
            jsonData = decodeURIComponent(jsonData);
            var obj = JSON.parse(jsonData);
            for (var i = 0; i < obj.mylist.length; i++) {
                msgArr.push({msg:obj.mylist[i].msg, index:obj.mylist[i].index});
                indexArr.push(obj.mylist[i].index);
            }
            if (obj.mylist.length > 0) {
                saveMsg();
            }
//            $('.listCollap').remove();
            for (var i = msgArr.length - 1; i >= 0; i--) {
//                $('<div data-role="collapsible" data-theme="c" data-content-theme="d" class="listCollap"><h3>' + msgArr[i].msg + '</h3><p>' + msgArr[i].msg + '</p></div>').appendTo('#myListView');
           $('<li>' + msgArr[i].msg + '</li>').appendTo('#myListView');
            //              $('#myListView').append('<li><a href="#" onClick="clickMyList(' + i + ', ' + obj.mylist[i].index + ')">' + obj.mylist[i].msg + '</a></li>');
            }
           $('#myListView').listview('refresh');
           //setTimeout(function() {myScroll.refresh();}, 800);
           
//           alert(myScroll.minScrollY + "_" + myScroll.maxScrollY);
//           loaded();
//            $('.listCollap').collapsible({refresh:true});
        }
    });
}
function compareTotals(a, b) {
    return b.index - a.index;
}
function saveMsg()
{
    window.localStorage.setItem("indexArr", indexArr.join(","));
    for (i = 0; i < indexArr.length; i++) {
        window.localStorage.setItem("msg" + indexArr[i], msgArr[i].msg);
    }
}
function deleteMsg($index)
{
    for(i = 0; i < indexArr.length; i++) 
    {
        if (indexArr[i] == $index) {
            indexArr.splice(i, 1);
            msgArr.splice(i, 1);
            break;
        }
    }
    saveMsg();
}
function clickMyList(num, index)
{
    $.mobile.changePage('view.html', {data:index});
//    alert("clickMyList = " + num + "_" + index);
}
function goMylist()
{
	$.mobile.changePage("mylist.html");
}

function goWrite()
{
	PhoneGap.exec(getContact, error, "OpenContact", "getContact", []);
}
function error(msg)
{
    alert(msg);
}
function getContact(data)
{
	if (data.result == "1")
	{
		receiver = data.name;
		receiverNum = delHyphen(data.phone);
		$.mobile.changePage("write.html");
	}
}

function checkRead()
{
	alert("checkRead");
}

function setting()
{
	$.mobile.changePage("setting.html");
}